import "../../scss/register.scss";
import $ from "jquery";
require("../utils/remove_ad");
import "../utils/city/jquery.cityselect";
import autosize from "../utils/autosize";
import com from "../utils/common";

$(function () {
    let uid = 0;
    $(".protocol_book").click(function (event) {
        event.stopPropagation();
        $("#layui-m-layer5").css("display", "block");
    });
    $(".layui-m-layershade ,.layui-m-layerbtn").click(function () {
        $("#layui-m-layer5").css("display", "none");
    });

    $(".mask,.publish_act").click(function (event) {
        event.stopPropagation();
        $(".mask").css("display", "none");
        $(".alert_prompt").css("display", "none");
    });
    $(".publish_act").click(function (event) {
        event.stopPropagation();
        window.location.href = "/center";
    });

    // 点击出现二维码
    $(".QR_code").click(function () {
        $(".unfolded_QR_code").show();
        $(".layui-m-layershade").click(function () {
            $(".unfolded_QR_code").hide();
        });
    });


    // 阻止事件冒泡
    $(".sidebar_msg").click(function (event) {
        event.stopPropagation();
    });
    $("#chooseplay").citySelect({
        prov: "北京",
        nodata: "none"
    });

    let user = {

        showCenterPops: function (ele) {
            // 显示弹窗,并让其显示在中间
            $(".mask").show();
            ele.show();
            let eleHeight = ele.height();
            ele.css({"margin-top": -eleHeight / 2 + "px", top: "50%"});
        },
        setTime: function (ele, time) {  // 设置验证码有效倒计时
            // 多久后获取验证码
            if (time === 0) {
                $(".verify_code").click(user.getVerifyCode);
                ele.css("color", "#0099ff").text("获取验证码");
                // 如果不写return将会一直重复执行这边代码
                return;
            } else {
                ele.css("color", "#969696").text(time + "s后重发");
                time--;
                $(".verify_code").unbind();
            }
            setTimeout(function () {
                user.setTime(ele, time);
            }, 1000);
        },

        getVerifyCode: function () {
            if (com.CheckMobile($(".verify_phone").val())) {
                let countdown = 60;
                let data = $("form").serialize();

                user.dialog("验证短信已经发送至" + $(".verify_phone").val() + "，请注意查收.");
                let ele = $(".verify_code");
                user.setTime(ele, countdown);
                /* $.ajax({
                 type: "POST",
                 data: data,
                 url: "/user/pre_reg/" +
                 $(".verify_phone")
                 .val()
                 .trim(),
                 dataType: "json",
                 success: function (data) {
                 if (data.status) {
                 user.dialog("验证短信已经发送至" + $(".verify_phone").val() + "，请注意查收.");
                 user.setTime(".verify_code", countdown);
                 } else {
                 user.dialog(data.msg);
                 }
                 }
                 });*/
            } else {
                user.dialog("请输入正确手机号");

            }
        },

        dialog: function (msg) {   // 弹窗显示
            $(".js_tooltips")
                .text(msg)
                .css("display", "block");
            setTimeout(function () {
                $(".js_tooltips").css("display", "none");
            }, 5000);
        },
        login: function () {  // 注册

            let publish = $(this);
            if ($("input[type='checkbox']").is(":checked") === false) {
                user.dialog("请先同意用户协议");
                return;
            }
            if (!com.CheckMobile($(".verify_phone").val())) {
                user.dialog("请输入正确手机号");
                return;
            }
            /*
            let password = $.trim($('.password').val());
            console.log(password.length);
            if(password.length < 6 || password.length > 20){
                user.dialog("建议密码6-20位,不包含空格");
                return;
            }
            let Organization = $('.Organization').val();
            if(Organization.length <= 0){
                user.dialog("请输入机构名称");
                return;
            }
            let Contact = $('.Contact').val();
            if(Contact.length <= 0){
                user.dialog("请输入联系人名称");
                return;
            }
            let verifyCode = $('.verifyCode').val();
            if(verifyCode.length != 4){
                user.dialog("请输入正确验证码");
                return;
            }
            */
            com.ajax({
                url: "/user/reg",
                data: $("#regform").serialize(),
                method: "post",
                ok: function (data) {
                    console.log(data);
                    user.showCenterPops($('.alert_prompt'));
                    $('.mask').click(function(){
                        window.location.href = "user_page/user_center.html";
                    })
                    /*
                    $(".business_msg").css("display", "none");
                    $(".business_add").css("display", "block");
                    publish.text("完善信息发布活动").unbind().click(function(){
                       user.addMsg();
                    });
                    $(".express_login")
                        .text("先发布活动");
                    $(".header > p")
                        .text("完善以下资料我们会给你推荐精准营销方案哦！")
                        .css("text-align", "center");

                        */
                },
                fail: function (data) {
                    console.log(data);
                }
            });
        },
        addMsg:function(){
            let Industry = $('.Industry').val();
            if(Industry <= 0){
                user.dialog("请选择行业信息");
                return;
            }
            let Phone = $(".verify_phone").val();
            let addMsg = $("#addMsg").serialize() + '&Phone' + Phone;
            com.ajax({
                url: "/user/reg",
                data: addMsg,
                method: "post",
                ok: function (data) {

                },
                fail: function (data) {
                    console.log(data);
                }
            });
        }
    };


    $(".publish_events").click(user.login);   // 登录

    $(".verify_code").click(user.getVerifyCode);  //获取验证码


});
